package com.cennavi.minenavidemo.activity.geocode;

import android.graphics.Point;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.cennavi.minenavidemo.R;
import com.minedata.minenavi.map.MapView;
import com.minedata.minenavi.map.Marker;
import com.minedata.minenavi.map.MarkerOptions;
import com.minedata.minenavi.map.MineMap;
import com.minedata.minenavi.mapdal.LatLng;
import com.minedata.minenavi.mapdal.Tools;
import com.minedata.minenavi.poiquery.GeocodeAddress;
import com.minedata.minenavi.poiquery.GeocodeQuery;
import com.minedata.minenavi.poiquery.GeocodeResult;
import com.minedata.minenavi.poiquery.GeocodeSearch;
import com.minedata.minenavi.poiquery.RegeocodeResult;
import com.minedata.minenavi.poiquery.SearchErrorCode;

import java.util.List;

/**
 * 地理编码查询
 */
public class GeocodeActivity extends AppCompatActivity {
    private MapView mMapView;
    private MineMap mMineMap;
    public Marker marker;
    private GeocodeSearch geocoderSearch;
    private EditText etCityCode, etAddress;
    private TextView result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_geocode);
        initE();
        initView();
    }

    private void initView() {
        etCityCode = findViewById(R.id.etCityCode);
        etAddress = findViewById(R.id.etAddress);
        result = findViewById(R.id.result);
        findViewById(R.id.search).setOnClickListener(v -> geocodeSearch(v));

        mMapView = findViewById(R.id.mapView);

        mMapView.addMapRenderCallback(mineMap -> {
            mMineMap = mineMap;
            mMineMap.setMinZoomLevel(0.0f);
            mMineMap.setMaxZoomLevel(17.0f);
            Point point = new Point(11630777, 3998405);
            mMineMap.setPointToCenter(point.x, point.y);
            mMineMap.setZoomLevel(11);
        });
    }

    private void initE() {
        geocoderSearch = new GeocodeSearch(this);
        geocoderSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
            @Override
            public void onGeocodeSearched(GeocodeResult geocodeResult, int resultID) {
                if (resultID == SearchErrorCode.none) {
                    List<GeocodeAddress> geocodeAddressList = geocodeResult.getGeocodeAddressList();

                    if (geocodeAddressList != null && geocodeAddressList.size() > 0) {
                        GeocodeAddress geocodeAddress = geocodeAddressList.get(0);
                        double latitude = geocodeAddress.getLatLonPoint().getLatitude();
                        double longitude = geocodeAddress.getLatLonPoint().getLongitude();

                        result.setText("位置：" + longitude + " , " + latitude);

                        Point point = Tools.latLngToPoint(new LatLng(latitude, longitude));
                        mMineMap.setPointToCenter(point.x, point.y);

                        if (marker != null) {
                            mMineMap.removeMarker(marker);
                        }

                        LatLng latLng = new LatLng(latitude, longitude);
                        marker = mMineMap.addMarker(new MarkerOptions().zLevel(7).position(latLng).iconId(1415).infoWindowEnable(false));
                    }
                }
            }

            @Override
            public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int resultID) {
            }
        });
    }

    private void geocodeSearch(View view) {
        String address = etAddress.getText().toString();
        if (TextUtils.isEmpty(address)) {
            Toast.makeText(this, "请输入地址", Toast.LENGTH_LONG).show();
            return;
        }
        String cityCode = etCityCode.getText().toString();
        if (TextUtils.isEmpty(cityCode)) {
            Toast.makeText(this, "请输入城市码", Toast.LENGTH_LONG).show();
            return;
        }

        geocode(address.trim(), cityCode);
    }

    private void geocode(String address, String cityCode) {
        GeocodeQuery query = new GeocodeQuery(address, cityCode);
        geocoderSearch.getFromLocationNameAsyn(query);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
